/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package xcore.model.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

import xcore.constraint.XIntVar;

import xcore.model.ModelPackage;
import xcore.model.XAttribute;
import xcore.model.XClass;
import xcore.model.XObject;
import xcore.model.XReference;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>XClass</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link xcore.model.impl.XClassImpl#isAbstract <em>Abstract</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getXSuperClass <em>XSuper Class</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getXSubClasses <em>XSub Classes</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getCardinality <em>Cardinality</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getXReferences <em>XReferences</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getSelfCardinlity <em>Self Cardinlity</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getFirstObject <em>First Object</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getXAttributes <em>XAttributes</em>}</li>
 *   <li>{@link xcore.model.impl.XClassImpl#getXObjects <em>XObjects</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class XClassImpl extends XElementImpl implements XClass {
	/**
	 * The default value of the '{@link #isAbstract() <em>Abstract</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAbstract()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ABSTRACT_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isAbstract() <em>Abstract</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAbstract()
	 * @generated
	 * @ordered
	 */
	protected boolean abstract_ = ABSTRACT_EDEFAULT;

	/**
	 * The cached value of the '{@link #getXSuperClass() <em>XSuper Class</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXSuperClass()
	 * @generated
	 * @ordered
	 */
	protected XClass xSuperClass;

	/**
	 * The cached value of the '{@link #getXSubClasses() <em>XSub Classes</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXSubClasses()
	 * @generated
	 * @ordered
	 */
	protected EList<XClass> xSubClasses;

	/**
	 * The cached value of the '{@link #getCardinality() <em>Cardinality</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCardinality()
	 * @generated
	 * @ordered
	 */
	protected XIntVar cardinality;

	/**
	 * The cached value of the '{@link #getXReferences() <em>XReferences</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXReferences()
	 * @generated
	 * @ordered
	 */
	protected EList<XReference> xReferences;

	/**
	 * The cached value of the '{@link #getSelfCardinlity() <em>Self Cardinlity</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSelfCardinlity()
	 * @generated
	 * @ordered
	 */
	protected XIntVar selfCardinlity;

	/**
	 * The default value of the '{@link #getFirstObject() <em>First Object</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFirstObject()
	 * @generated
	 * @ordered
	 */
	protected static final int FIRST_OBJECT_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getFirstObject() <em>First Object</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFirstObject()
	 * @generated
	 * @ordered
	 */
	protected int firstObject = FIRST_OBJECT_EDEFAULT;

	/**
	 * The cached value of the '{@link #getXAttributes() <em>XAttributes</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXAttributes()
	 * @generated
	 * @ordered
	 */
	protected EList<XAttribute> xAttributes;

	/**
	 * The cached value of the '{@link #getXObjects() <em>XObjects</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXObjects()
	 * @generated
	 * @ordered
	 */
	protected EList<XObject> xObjects;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XClassImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelPackage.Literals.XCLASS;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isAbstract() {
		return abstract_;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAbstract(boolean newAbstract) {
		boolean oldAbstract = abstract_;
		abstract_ = newAbstract;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__ABSTRACT, oldAbstract, abstract_));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XClass getXSuperClass() {
		if (xSuperClass != null && xSuperClass.eIsProxy()) {
			InternalEObject oldXSuperClass = (InternalEObject)xSuperClass;
			xSuperClass = (XClass)eResolveProxy(oldXSuperClass);
			if (xSuperClass != oldXSuperClass) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ModelPackage.XCLASS__XSUPER_CLASS, oldXSuperClass, xSuperClass));
			}
		}
		return xSuperClass;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XClass basicGetXSuperClass() {
		return xSuperClass;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetXSuperClass(XClass newXSuperClass, NotificationChain msgs) {
		XClass oldXSuperClass = xSuperClass;
		xSuperClass = newXSuperClass;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__XSUPER_CLASS, oldXSuperClass, newXSuperClass);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setXSuperClass(XClass newXSuperClass) {
		if (newXSuperClass != xSuperClass) {
			NotificationChain msgs = null;
			if (xSuperClass != null)
				msgs = ((InternalEObject)xSuperClass).eInverseRemove(this, ModelPackage.XCLASS__XSUB_CLASSES, XClass.class, msgs);
			if (newXSuperClass != null)
				msgs = ((InternalEObject)newXSuperClass).eInverseAdd(this, ModelPackage.XCLASS__XSUB_CLASSES, XClass.class, msgs);
			msgs = basicSetXSuperClass(newXSuperClass, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__XSUPER_CLASS, newXSuperClass, newXSuperClass));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XClass> getXSubClasses() {
		if (xSubClasses == null) {
			xSubClasses = new EObjectWithInverseResolvingEList<XClass>(XClass.class, this, ModelPackage.XCLASS__XSUB_CLASSES, ModelPackage.XCLASS__XSUPER_CLASS);
		}
		return xSubClasses;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XIntVar getCardinality() {
		return cardinality;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetCardinality(XIntVar newCardinality, NotificationChain msgs) {
		XIntVar oldCardinality = cardinality;
		cardinality = newCardinality;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__CARDINALITY, oldCardinality, newCardinality);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setCardinality(XIntVar newCardinality) {
		if (newCardinality != cardinality) {
			NotificationChain msgs = null;
			if (cardinality != null)
				msgs = ((InternalEObject)cardinality).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XCLASS__CARDINALITY, null, msgs);
			if (newCardinality != null)
				msgs = ((InternalEObject)newCardinality).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XCLASS__CARDINALITY, null, msgs);
			msgs = basicSetCardinality(newCardinality, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__CARDINALITY, newCardinality, newCardinality));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XReference> getXReferences() {
		if (xReferences == null) {
			xReferences = new EObjectWithInverseResolvingEList<XReference>(XReference.class, this, ModelPackage.XCLASS__XREFERENCES, ModelPackage.XREFERENCE__XCLASS);
		}
		return xReferences;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XIntVar getSelfCardinlity() {
		return selfCardinlity;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetSelfCardinlity(XIntVar newSelfCardinlity, NotificationChain msgs) {
		XIntVar oldSelfCardinlity = selfCardinlity;
		selfCardinlity = newSelfCardinlity;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__SELF_CARDINLITY, oldSelfCardinlity, newSelfCardinlity);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSelfCardinlity(XIntVar newSelfCardinlity) {
		if (newSelfCardinlity != selfCardinlity) {
			NotificationChain msgs = null;
			if (selfCardinlity != null)
				msgs = ((InternalEObject)selfCardinlity).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XCLASS__SELF_CARDINLITY, null, msgs);
			if (newSelfCardinlity != null)
				msgs = ((InternalEObject)newSelfCardinlity).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - ModelPackage.XCLASS__SELF_CARDINLITY, null, msgs);
			msgs = basicSetSelfCardinlity(newSelfCardinlity, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__SELF_CARDINLITY, newSelfCardinlity, newSelfCardinlity));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getFirstObject() {
		return firstObject;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFirstObject(int newFirstObject) {
		int oldFirstObject = firstObject;
		firstObject = newFirstObject;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XCLASS__FIRST_OBJECT, oldFirstObject, firstObject));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XAttribute> getXAttributes() {
		if (xAttributes == null) {
			xAttributes = new EObjectWithInverseResolvingEList<XAttribute>(XAttribute.class, this, ModelPackage.XCLASS__XATTRIBUTES, ModelPackage.XATTRIBUTE__XCLASS);
		}
		return xAttributes;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XObject> getXObjects() {
		if (xObjects == null) {
			xObjects = new EObjectWithInverseResolvingEList<XObject>(XObject.class, this, ModelPackage.XCLASS__XOBJECTS, ModelPackage.XOBJECT__XCLASS);
		}
		return xObjects;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XCLASS__XSUPER_CLASS:
				if (xSuperClass != null)
					msgs = ((InternalEObject)xSuperClass).eInverseRemove(this, ModelPackage.XCLASS__XSUB_CLASSES, XClass.class, msgs);
				return basicSetXSuperClass((XClass)otherEnd, msgs);
			case ModelPackage.XCLASS__XSUB_CLASSES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getXSubClasses()).basicAdd(otherEnd, msgs);
			case ModelPackage.XCLASS__XREFERENCES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getXReferences()).basicAdd(otherEnd, msgs);
			case ModelPackage.XCLASS__XATTRIBUTES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getXAttributes()).basicAdd(otherEnd, msgs);
			case ModelPackage.XCLASS__XOBJECTS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getXObjects()).basicAdd(otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XCLASS__XSUPER_CLASS:
				return basicSetXSuperClass(null, msgs);
			case ModelPackage.XCLASS__XSUB_CLASSES:
				return ((InternalEList<?>)getXSubClasses()).basicRemove(otherEnd, msgs);
			case ModelPackage.XCLASS__CARDINALITY:
				return basicSetCardinality(null, msgs);
			case ModelPackage.XCLASS__XREFERENCES:
				return ((InternalEList<?>)getXReferences()).basicRemove(otherEnd, msgs);
			case ModelPackage.XCLASS__SELF_CARDINLITY:
				return basicSetSelfCardinlity(null, msgs);
			case ModelPackage.XCLASS__XATTRIBUTES:
				return ((InternalEList<?>)getXAttributes()).basicRemove(otherEnd, msgs);
			case ModelPackage.XCLASS__XOBJECTS:
				return ((InternalEList<?>)getXObjects()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ModelPackage.XCLASS__ABSTRACT:
				return isAbstract() ? Boolean.TRUE : Boolean.FALSE;
			case ModelPackage.XCLASS__XSUPER_CLASS:
				if (resolve) return getXSuperClass();
				return basicGetXSuperClass();
			case ModelPackage.XCLASS__XSUB_CLASSES:
				return getXSubClasses();
			case ModelPackage.XCLASS__CARDINALITY:
				return getCardinality();
			case ModelPackage.XCLASS__XREFERENCES:
				return getXReferences();
			case ModelPackage.XCLASS__SELF_CARDINLITY:
				return getSelfCardinlity();
			case ModelPackage.XCLASS__FIRST_OBJECT:
				return new Integer(getFirstObject());
			case ModelPackage.XCLASS__XATTRIBUTES:
				return getXAttributes();
			case ModelPackage.XCLASS__XOBJECTS:
				return getXObjects();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ModelPackage.XCLASS__ABSTRACT:
				setAbstract(((Boolean)newValue).booleanValue());
				return;
			case ModelPackage.XCLASS__XSUPER_CLASS:
				setXSuperClass((XClass)newValue);
				return;
			case ModelPackage.XCLASS__XSUB_CLASSES:
				getXSubClasses().clear();
				getXSubClasses().addAll((Collection<? extends XClass>)newValue);
				return;
			case ModelPackage.XCLASS__CARDINALITY:
				setCardinality((XIntVar)newValue);
				return;
			case ModelPackage.XCLASS__XREFERENCES:
				getXReferences().clear();
				getXReferences().addAll((Collection<? extends XReference>)newValue);
				return;
			case ModelPackage.XCLASS__SELF_CARDINLITY:
				setSelfCardinlity((XIntVar)newValue);
				return;
			case ModelPackage.XCLASS__FIRST_OBJECT:
				setFirstObject(((Integer)newValue).intValue());
				return;
			case ModelPackage.XCLASS__XATTRIBUTES:
				getXAttributes().clear();
				getXAttributes().addAll((Collection<? extends XAttribute>)newValue);
				return;
			case ModelPackage.XCLASS__XOBJECTS:
				getXObjects().clear();
				getXObjects().addAll((Collection<? extends XObject>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ModelPackage.XCLASS__ABSTRACT:
				setAbstract(ABSTRACT_EDEFAULT);
				return;
			case ModelPackage.XCLASS__XSUPER_CLASS:
				setXSuperClass((XClass)null);
				return;
			case ModelPackage.XCLASS__XSUB_CLASSES:
				getXSubClasses().clear();
				return;
			case ModelPackage.XCLASS__CARDINALITY:
				setCardinality((XIntVar)null);
				return;
			case ModelPackage.XCLASS__XREFERENCES:
				getXReferences().clear();
				return;
			case ModelPackage.XCLASS__SELF_CARDINLITY:
				setSelfCardinlity((XIntVar)null);
				return;
			case ModelPackage.XCLASS__FIRST_OBJECT:
				setFirstObject(FIRST_OBJECT_EDEFAULT);
				return;
			case ModelPackage.XCLASS__XATTRIBUTES:
				getXAttributes().clear();
				return;
			case ModelPackage.XCLASS__XOBJECTS:
				getXObjects().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ModelPackage.XCLASS__ABSTRACT:
				return abstract_ != ABSTRACT_EDEFAULT;
			case ModelPackage.XCLASS__XSUPER_CLASS:
				return xSuperClass != null;
			case ModelPackage.XCLASS__XSUB_CLASSES:
				return xSubClasses != null && !xSubClasses.isEmpty();
			case ModelPackage.XCLASS__CARDINALITY:
				return cardinality != null;
			case ModelPackage.XCLASS__XREFERENCES:
				return xReferences != null && !xReferences.isEmpty();
			case ModelPackage.XCLASS__SELF_CARDINLITY:
				return selfCardinlity != null;
			case ModelPackage.XCLASS__FIRST_OBJECT:
				return firstObject != FIRST_OBJECT_EDEFAULT;
			case ModelPackage.XCLASS__XATTRIBUTES:
				return xAttributes != null && !xAttributes.isEmpty();
			case ModelPackage.XCLASS__XOBJECTS:
				return xObjects != null && !xObjects.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (abstract: ");
		result.append(abstract_);
		result.append(", firstObject: ");
		result.append(firstObject);
		result.append(')');
		return result.toString();
	}

} //XClassImpl
